package com.googlecode.tinyjava.symboltable;

public class SymbolTable {
	/**
	 * @uml.property  name="rootScope"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	public Scope rootScope;
	/**
	 * @uml.property  name="currentScope"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	public Scope currentScope;
	
	public SymbolTable(Scope scope) {
		this.rootScope = scope;
		this.currentScope = scope;
	}

	public void print() {
		rootScope.print(0);
	}

	public void add(NamedItem item) {
		NamedItem local = currentScope.getLocal(item.name);
		if (local == null) {
			currentScope.append(item);
			if (item instanceof Scope) {
				Scope scope = (Scope) item;
				scope.outer = currentScope;
				currentScope = scope;
			}
		} else {
			local.printError();
		}
	}
	
	public Scope getScope(String name) {
		currentScope = (Scope)currentScope.getLocal(name);
		return currentScope;
	}
	
	public NamedItem get(String name) {
		return currentScope.get(name);
	}
	
	public void back() {
		this.currentScope = this.currentScope.outer;
	}
}
